package com.homer.android.sdk.command.user;

import org.apache.http.HttpStatus;
import org.json.JSONException;
import org.json.JSONObject;







import com.homer.android.sdk.Homer;
import com.homer.android.sdk.command.IEspCommandUserLoginPhoneInternet;
import com.homer.android.sdk.type.user.EspLoginResult;
import com.homer.android.sdk.type.user.builder.BEspUser;
import com.homer.android.sdk.user.IEspUser;
import com.homer.iot.esptouch.base.api.EspBaseApiUtil;



public class EspCommandUserLoginPhoneInternet implements IEspCommandUserLoginPhoneInternet{
	@Override
    public EspLoginResult doCommandUserLoginPhone(String phoneNumber, String userPassword)
    {
        try
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(User_Phone, phoneNumber);
            jsonObject.put(User_Password, userPassword);
            jsonObject.put(APP_KEY,BEspUser.getBuilder().getInstance().getAppKey());
            JSONObject jsonObjectResult = EspBaseApiUtil.Post(URL, jsonObject);
            EspLoginResult result = null;
            if (jsonObjectResult == null)
            {
                return EspLoginResult.NETWORK_UNACCESSIBLE;
            }
            int status = jsonObjectResult.getInt(Status);
            if (status == HttpStatus.SC_OK)
            {
            	String userToken = jsonObjectResult.getString("userToken");
            	String userObj = jsonObjectResult.getString(USER);            	
            	JSONObject userJSON = new JSONObject(userObj);
            	//{"id":1,"username":"yourphone","password":"637B9ADADF7ACCE5C70E5D327A725B13","name":"yourphone","createdDate":"Apr 25, 2016 10:32:02 PM","phone":"yourphone","userToken":"0AEA341765556184AB047166EB7EDBEC","online":false}
            	//{id=1, username=yourphone, password=637B9ADADF7ACCE5C70E5D327A725B13, name=yourphone, createdDate=Apr 25, 2016 10:32:02 PM, phone=yourphone, userToken=0AEA341765556184AB047166EB7EDBEC, online=false}
            	
                long userId = userJSON.getLong(Id);
                String userName = userJSON.getString(User_Name);
              
                //String email = userJSON.getString(User_Email);
                String name = userJSON.getString("name");
                String created_date = userJSON.getString("createdDate");              
                String phone = userJSON.getString("phone");
                //JSONObject keyJSON = jsonObjectResult.getJSONObject(Key);
                //String userKey = keyJSON.getString(Token);
                
                IEspUser user = BEspUser.getBuilder().getInstance();
                user.setUserKey(userToken);              
                user.setUserId(userId);
                user.setUserName(userName);
                user.setPhone(phone);
                //user.setUserEmail(email);
            }
            else
            {
            }
            result = EspLoginResult.getEspLoginResult(status);
            return result;
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        return EspLoginResult.NETWORK_UNACCESSIBLE;
    }
}
